
package com.idealighter.game.roomcard.games.mustniuniu.struct;

import com.idealighter.game.games.common.SeatState;
import com.idealighter.game.games.common.niuniu.NiuNiuCard;
import com.idealighter.game.roomcard.common.AbstractRoomCardSeat;
import com.idealighter.game.roomcard.games.mustniuniu.bo.BetTypeBo;
import com.idealighter.game.roomcard.games.mustniuniu.constant.MustNiuNiuCardType;
import com.idealighter.game.roomcard.struct.roomcfg.MustNiuNiuRoomCardRoomCfg;
import com.idealighter.game.roomcard.struct.roomcfg.value.MustNiuNiuRoomCardRoomCfgValue;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ScheduledFuture;

import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * 四人牛牛座位 .
 * 
 * @date 2016年1月20日 下午5:05:39
 *
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class MustNiuNiuSeat
    extends AbstractRoomCardSeat<MustNiuNiuRoomCardRoomCfg, MustNiuNiuRoomCardRoomCfgValue> {
  // 所属桌子
  private final MustNiuNiuTable table;
  // 是否是庄家
  private boolean dealer = false;
  // 叫庄倍数(null:未叫庄0:不叫庄)
  private Integer callDealerMultiple = null;
  // 下注自己的筹码
  private long betedchips = 0;
  // 当前结算筹码
  private long billChips = 0;
  // 玩家的牌数据
  private final List<NiuNiuCard> cards = new ArrayList<>();
  // 玩家的牌最优牌(3+2)组合
  private final List<NiuNiuCard> bestCards = new ArrayList<>();
  // 当前牌型
  private MustNiuNiuCardType cardsType = null;
  // 是否已经摊牌
  private boolean showdown = false;
  // 牛牛步骤(准备、叫庄、下注、摊牌)定时Future
  public ScheduledFuture<?> stepFuture = null;
  // 手牌大小(比庄家小设置为-1,比庄家大设置为1,庄家为0)
  private int power = 0;

  // ==== 推注选项
  private Long preWinBetSelf = 0L;

  // ==== 用于闲家买码
  private Long betOtherPlayerId = null; // 买码玩家,null:未操作,0:不买
  private long betOtherBetChips = 0L; // 买码筹码
  private long betOtherWinChips = 0L; // 买码后赢的筹码

  // ==== 加倍
  private boolean doubleBet = false;
  private BetTypeBo betType; // 下注类型

  private Boolean answerCloseRoom = null; // 是否应答
  private Boolean hadApplyCloseRoom = null; // 是否申请关闭


  /**
   * 构造函数 .
   * 
   * @param order .
   * @param table .
   */
  public MustNiuNiuSeat(int order, MustNiuNiuTable table) {
    super(order, table);
    this.table = table;
  }

  /**
   * 重置座位数据 .
   */
  public void reset() {
    this.dealer = false;
    this.callDealerMultiple = null;
    this.betedchips = 0;
    this.billChips = 0;
    this.totalChips = 0;
    this.power = 0;
    this.cards.clear();
    this.bestCards.clear();
    this.cardsType = null;
    this.state = SeatState.SEATED;
    this.showdown = false;
    if (this.stepFuture != null) {
      this.stepFuture.cancel(false);
    }
    this.betOtherPlayerId = null;
    this.betOtherBetChips = 0L;
    this.betOtherWinChips = 0L;
    this.doubleBet = false;
  }

  public int getPower() {
    return power;
  }

  public void setPower(int power) {
    this.power = power;
  }

  /**
   * 重置座位数据除了totalChips .
   */
  public void resetButTotalChips() {
    long totalChips = this.totalChips;
    reset();
    this.totalChips = totalChips;
  }

  /**
   * 清空座位数据 .
   */
  @Override
  public void clear() {
    super.clear();
    reset();

    if (table.players().size() <= 1) {
      boolean readied = table.isReadied();
      table.reset();
      table.setReadied(readied);
    }
  }

  @SuppressWarnings("unchecked")
  @Override
  public MustNiuNiuTable table() {
    return table;
  }
}
